package com.zhouxiaoge.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/***************************************************
 *                    _ooOoo_                  *   
 *                   o8888888o                 * 佛        
 *                  88'' . ''88                * 祖 
 *                  ( | -_- | )                * 保 
 *                   0\  =  /0                 * 佑 
 *                ____/'---'\____              *    
 *              .'  \\|     |//  '.            * 永 
 *             /  \\|||  :  |||//  \           * 无 
 *            /  _||||| -:- |||||-  \          * BUG
 *            |   | \\\  -  /// |   |          *
 *            | \_|  ''\---/''  |   |          * 阿
 *            \  .-\__  `-`  ___/-. /          * 弥
 *          ___`. .'  /--.--\  `. . __         * 陀
 *       ."" '<  ‘.___\_<|>_/___.’  >'"".      * 佛
 *      | | :  ` - `.;`\ _ /`;.`/ - ` : | |    *
 *      \  \ `-.   \_ __\ /__ _/   .-` /  /    * 
 * ======`-.____`-.___\_____/___.-`____.-'=====*
 * @author 周明阳 mail:gqzmy@outlook.com 时间:2018-01-16 10:19
 */
public class TraceHandler implements InvocationHandler {

    private Object target;

    public TraceHandler(Object t) {
        target = t;
    }

    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
        // print implicit argument
        System.out.print("======" + target + "======");
        // print method name
        System.out.print("." + m.getName() + "(");
        // print explicit arguments
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                System.out.print(args[i]);
                if (i < args.length - 1) {
                    System.out.print(",,,,,");
                }
            }
        }
        System.out.println(")");
        // invoke actual method
        return m.invoke(target, args);
    }

}
